<?php

namespace App\Remote\WhoBot;


use App\Exceptions\BotException\BotException;
use App\Exceptions\BotException\WhoBotException;
use Closure;
use Symfony\Component\HttpFoundation\Response as ResponseAlias;

class Remote extends \App\Remote\Remote
{
    static function getBaseUri(): string
    {
        return config('aiBot.whoBot.domain');
    }

    public static function buildBasicHeaders(): array
    {
        return [
            'X_BD_LOGID'   => time(),
            "content-type" => "application/json"
        ];
    }

    public static function registerResponseHandle(): Closure
    {
        return function ($responseHttpCode, $responseOriginalContents) {

            if ($responseHttpCode == ResponseAlias::HTTP_OK && isJson($responseOriginalContents)) {
                $responseArr = jsonToArr($responseOriginalContents);
                if ($responseArr["response_status"] == "200") {
                    return $responseArr;
                }
                throw new WhoBotException(vsprintf(
                    "tid:%s | info:%s", [
                        attributeGet($responseArr, "trace_id", "-"),
                        attributeGet($responseArr, "reason", "-")
                    ]
                ));
            }
            throw new WhoBotException($responseOriginalContents);
        };
    }
}
